<?php

namespace app\rent\controller;

use think\Controller;

class Business extends Controller
{
    protected $BusinessModel;
    protected $CollectionModel;
    protected $RecordModel;
    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        // 用户模型
        $this->BusinessModel = model('Business.Business');
        $this->CollectionModel = model('Business.Collection');
        $this->RecordModel = model('Business.Record');
    }
    // 登录
    public function login()
    {
        if ($this->request->isPost()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $result = $this->BusinessModel->where('mobile', $mobile)->find();
            if (!$result) {
                $this->error('没有该用户');
            } else {
                $pwd = $result['password'];
                $password = $this->request->param('password', '', 'trim');
                $salt = $result['salt'];
                $password = md5($password . $salt);
                if ($pwd == $password) {
                    // 存储用户信息
                    $cateCount = $this->CollectionModel->Where(['busid' => $result['id'], 'cateid' => ['<>', 'NULL']])->count();
                    $proCount = $this->CollectionModel->Where(['busid' => $result['id'], 'proid'  => ['<>', 'NULL']])->count();
                    $data = [
                        'id' => $result['id'],
                        'mobile' => $result['mobile'],
                        'nickname' => $result['nickname'],
                        'avatar' => $result['avatar_url'],
                        'gender' => $result['gender'],
                        'province' => $result['province'],
                        'city' => $result['city'],
                        'district' => $result['district'],
                        'createtime' => $result['createtime'],
                        'money' => $result['money'],
                        'email' => $result['email'],
                        'region_text' => $result['region_text'],
                        'filings' => config('site.beian'),
                        'cateCount' => $cateCount??0,
                        'proCount' => $proCount ?? 0
                    ];
                    $this->success('登录成功', '', $data);
                } else {
                    $this->error('密码错误！');
                }
            }
        }
    }
    // 注册
    public function register()
    {
        if ($this->request->isPost()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');
            $salt = randstr();
            $password = md5($password . $salt);
            $data = [
                'password' => $password,
                'mobile' => $mobile,
                'salt' => $salt,
                'nickname' => $mobile
            ];
            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);
            if (!$result) {
                // getError => 返回模型的错误信息
                $this->error($this->BusinessModel->getError());
            } else {
                $this->success('注册成功');
            }
        }
    }
    // 校验是否登录
    public function checklogin()
    {
        // 获取请求参数，设置默认值为空
        $mobile = $this->request->param('mobile', '', 'trim');
        $id = $this->request->param('id', 0, 'trim');
        $result = $this->BusinessModel->where(['mobile' => $mobile, 'id' => $id])->find();
        if (!$result) {
            $this->error('用户不存在');
        }
        $cateCount = $this->CollectionModel->Where(['busid' => $id, 'cateid' => ['<>', 'NULL']])->count();
        $proCount = $this->CollectionModel->Where(['busid' => $id, 'proid'  => ['<>', 'NULL']])->count();
        // 存储用户信息
        $data = [
            'id' => $result['id'],
            'mobile' => $result['mobile'],
            'nickname' => $result['nickname'],
            'avatar' => $result['avatar_url'],
            'gender' => $result['gender'],
            'province' => $result['province'],
            'city' => $result['city'],
            'district' => $result['district'],
            'createtime' => $result['createtime'],
            'money' => $result['money'],
            'email' => $result['email'],
            'region_text' => $result['region_text'],
            'filings' => config('site.beian'),
            'cateCount' => $cateCount??0,
            'proCount' => $proCount ?? 0,
            'telephone'=>config('site.telephone')
        ];
        $this->success('用户存在', "", $data);
    }
    // 修改用户信息
    public function profile()
    {
        // 获取所有参数
        $params = $this->request->param();

        $businessinfo = $this->BusinessModel->find($params['id']);

        if (!$businessinfo) {
            return error('用户不存在');
        }
        $data = [
            'nickname' => $params['nickname'],
            'email' => $params['email'],
            'gender' => $params['gender'],
            'province' => $params['province'],
            'city' => $params['city'],
            'district' => $params['district'],
        ];
        // 判断是否修改邮箱
        if ($params['email'] != $businessinfo['email'] && $businessinfo['auth'] == '1') {
            $data['auth'] = '0';
        }
        // 获取图片
        if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0) {
            $res = upload('avatar');
            if ($res['code'] == 0) {
                $this->error($res['msg']);
            }

            $data['avatar'] = $res['data'];
        }
        $password = $params['password'] ?? '';
        // 判断是否修改密码
        if ($password) {

            $salt = randstr();
            // 密码加密
            $password = md5($password . $salt);

            $data['salt'] = $salt;
            $data['password'] = $password;
        }
        $res = $this->BusinessModel->where('id', $params['id'])->update($data);
        if ($res === false) {
            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0) {
                $path = ltrim($data['avatar'], '/');
                is_file($path) && unlink($path);
            }
            $this->error("修改失败");
        } else {
            if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0) {
                if ($businessinfo['avatar']) {
                    // 删除旧的图片
                    $path = ltrim($businessinfo['avatar'], '/');
                    is_file($path) && unlink($path);
                }
            }
            $result = $this->BusinessModel->find($params['id']);
            $cateCount = $this->CollectionModel->Where(['busid' => $result['id'], 'cateid' => ['<>', 'NULL']])->count();
            $proCount = $this->CollectionModel->Where(['busid' => $result['id'], 'proid'  => ['<>', 'NULL']])->count();
            $data = [
                'id' => $result['id'],
                'mobile' => $result['mobile'],
                'nickname' => $result['nickname'],
                'avatar' => $result['avatar_url'],
                'gender' => $result['gender'],
                'province' => $result['province'],
                'city' => $result['city'],
                'district' => $result['district'],
                'createtime' => $result['createtime'],
                'money' => $result['money'],
                'email' => $result['email'],
                'region_text' => $result['region_text'],
                'filings' => config('site.beian'),
                'cateCount' => $cateCount??0,
                'proCount' => $proCount ?? 0
            ];
            $this->success('修改成功', '', $data);
        }
    }
    // 获取消费记录
    public function record(){
        $page = $this->request->param('page', 1, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');
        //   用户是否存在 
        $businessinfo = $this->BusinessModel->find($busid);
        if (!$businessinfo) {
            $this->error('用户不存在');
        }
        $list = $this->RecordModel->where(['busid'=>$busid])->page($page, 5)->order('createtime','desc')->select();
        if (count($list) > 0) {
            $this->success('', '', $list);
        } else {
            $this->error('暂无数据');
        }
    }
}
